//
//  ReadyActivityVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/26.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ReadyActivityVc.h"
#import "HomeYueViewCell.h"
#import "PersonNetWork.h"
#import <MJRefresh.h>
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "ConNearAcInfoViewController.h"
#import "QiandaoVC.h"

@interface ReadyActivityVc ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *sources;
@property (nonatomic,assign) NSInteger page;

@end

@implementation ReadyActivityVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"已报名活动";
    self.page = 1;
    [self.view addSubview:self.tableView];
    [self.tableView.mj_header beginRefreshing];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeYueViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeYueViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row];
    cell.isFromPerson = YES;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QiandaoVC *vc = [[QiandaoVC alloc]init];
    vc.model = self.sources[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
//    ConNearAcInfoViewController *infoVc = [[ConNearAcInfoViewController alloc]init];
//    HomeYueModel *model = self.sources[indexPath.row];
//    infoVc.ID = model.ID;
//    [self.navigationController pushViewController:infoVc animated:YES];
}
- (void)getAcList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.page);
    [PersonNetWork getMyReportListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hideNoDataLab];
        if (success) {
            self.tableView.mj_footer.hidden = (lists.count < 20);
            if (self.page == 1&&lists.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, (ScreenH-153)*0.5, ScreenW, 20) title:@"暂无已报名活动"];
                return ;
            }
            if (lists.count >= 20) {
                self.tableView.mj_footer.hidden = NO;
            }
            [self.sources addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = RGBColor(238, 238, 244);
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.rowHeight = 32+(ScreenW*230/750)*146/230;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeYueViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([HomeYueViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self.sources removeAllObjects];
            [self getAcList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page++;
            [self getAcList];
        }];
        _tableView.mj_footer.hidden = YES;
    }
    return _tableView;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
